package by.jabbby.service.listener;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.packet.MessageEvent;

import by.jabbby.Logger;
import by.jabbby.dataprovider.ChatEntry;
import by.jabbby.dataprovider.ChatProvider;
import by.jabbby.service.JabberService;
import by.jabbby.service.notification.NotificationBar;
import org.jivesoftware.smack.packet.Message.Type;;

 


public class JabberPacketListener implements PacketListener, PacketFilter {
private JabberService jabberService;

	public JabberPacketListener (JabberService js){
		jabberService = js;
	}
	
	public void processPacket(Packet packet) {		
		Logger.i("JabberPacketListener.processPacket()");
		try{
		if(packet instanceof Message){
			
			Message msg = (Message)packet;
			Boolean isExecuted = (Boolean) msg.getProperty("isExecuted");
			if(Boolean.TRUE.equals(isExecuted))  return;
			
			switch( msg.getType() ){
				case chat : processChat(msg); break;
				case error : processError(msg); break;
				case groupchat : processGroupChat(msg); break;
				case headline : processHeadLine(msg); break;
				case normal : processNormal(msg); break;
				default : Logger.e("JabberPacketListener.processPacket(): Unsupported message  type: " + msg.getType());
			}
			//msg.getExtension(elementName, namespace)
			
		/*	if("".equals(msg.getBody()) || msg.getBody()==null )return;
				
			String from = msg.getFrom();
			XMPPConnection connection = 
			 jabberService.getConnection();
			org.jivesoftware.smack.Chat chat = connection.getChatManager().getThreadChat(msg.getThread());
			if(chat==null){
				//chat = connection.getChatManager().createChat(jabberService.getAccount().getConnectionString(), jabberService.getAccount().getConnectionString(),this.jabberService);
				//jabberService.processMessage(chat, msg);
				ChatProvider cp = new ChatProvider(jabberService.getAccount().getConnectionString(), jabberService.getApplicationContext());
				cp.addEntry(new ChatEntry(msg.getBody(), msg.getFrom(),  jabberService.getAccount().getConnectionString()));
				NotificationBar nb = new NotificationBar();
				nb.notifi(jabberService.getApplicationContext(), msg.getFrom() );
				
			}
		*/
		}
		}catch(Exception e){
			Logger.e(e);
		}
		
			
		
	}
	
	private void processNormal(Message msg) {
		// TODO Auto-generated method stub
		
	}

	private void processHeadLine(Message msg) {
		// TODO Auto-generated method stub
		
	}

	private void processGroupChat(Message msg) {
		// TODO Auto-generated method stub
		
	}

	private void processError(Message msg) {
		// TODO Auto-generated method stub
		
	}

	private void processChat(Message msg){
		if("".equals(msg.getBody()) || msg.getBody()==null )return;
		
	//	String from = msg.getFrom();
		XMPPConnection connection = 
		 jabberService.getConnection();
		org.jivesoftware.smack.Chat chat = connection.getChatManager().getThreadChat(msg.getThread());
		if(chat==null){
			//chat = connection.getChatManager().createChat(jabberService.getAccount().getConnectionString(), jabberService.getAccount().getConnectionString(),this.jabberService);
			//jabberService.processMessage(chat, msg);
		//	ChatProvider cp = new ChatProvider(jabberService.getAccount().getConnectionString(), jabberService.getApplicationContext());
		//	cp.addEntry(new ChatEntry(msg.getBody(), msg.getFrom(),  jabberService.getAccount().getConnectionString()));
			NotificationBar nb = new NotificationBar();
			nb.notifi(jabberService.getApplicationContext(), msg.getFrom() );
		}
		ChatProvider cp = new ChatProvider(jabberService.getAccount().getConnectionString(), jabberService.getApplicationContext());
		cp.addEntry(new ChatEntry(msg.getBody(), msg.getFrom(),  jabberService.getAccount().getConnectionString()));
	}

	public boolean accept(Packet packet) {
		// TODO Auto-generated method stub
		return true;
	}
	
	private String getElementName(Message msg){
		String elem  = null;
		PacketExtension   pe = msg.getExtension("http://jabber.org/protocol/chatstates");
		
		if(pe instanceof DefaultPacketExtension){
			DefaultPacketExtension de = (DefaultPacketExtension) pe;
			 elem  = de.getElementName();	
		}
		return elem;
	}

}
